package com.thinksns.tschat.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ThinksnsTableSqlHelper extends SQLiteOpenHelper {
	public ThinksnsTableSqlHelper(Context context, String name,
								  CursorFactory factory, int version) {
		super(context, name, factory, version);
	}
	public static final String TAG = ThinksnsTableSqlHelper.class.getSimpleName();
	public static final String tbChatList = "tb_chat_List";// 聊天列表
	public static final String tbRoomList = "tb_room_List";// 聊天房间

	private static SQLiteDatabase db = null;
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		ThinksnsTableSqlHelper.db = db;
		System.out.println("oldVersion---------------onCreate------------- = " );
		Log.v(TAG, "onCreate");
	}
	
	/**
	 * 更新数据库的时候用下面的方法
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// db.execSQL("ALTER TABLE User ADD COLUMN myLastWeibo text");
		// db.execSQL("ALTER TABLE home_weibo ADD COLUMN weiboJson text");
		// db.execSQL("ALTER TABLE User ADD COLUMN userJson text");
		//4.5版本 hhj
		System.out.println("oldVersion---------------------------- = " + oldVersion);
		//聊天列表
		db.execSQL("ALTER TABLE "+tbChatList+" RENAME TO tb_chat_List_tmp");
		db.execSQL("CREATE TABLE if not exists "
				+ tbChatList
				+ "(_id INTEGER PRIMARY KEY AUTOINCREMENT, login_uid INTEGER, message_id INTEGER,from_uid INTEGER ,type TEXT,room_id INTEGER,"
				+ " content TEXT,uid INTEGER,from_uface TEXT,from_uname TEXT,attach_url TEXT,to_uid INTEGER,length INTEGER,latitude TEXT,longitude TEXT,location TEXT,attach_id TEXT,localPath TEXT,imgWidth TEXT," +
				"imgHeight TEXT,imgSendState TEXT,mtime INTEGER,card_uname TEXT,card_avatar TEXT,card_intro TEXT,card_uid INTEGER," +
				"notify_type TEXT,quit_uid INTEGER,quit_uname TEXT,room_master_uid INTEGER,title TEXT,room_add_uid INTEGER,room_add_uname TEXT," +
				"room_del_uid INTEGER,room_del_uname TEXT,isNew INTEGER,master_uname TEXT, description TEXT, is_send INTEGER, send_state INTEGER, pack_id TEXT,remark TEXT)");
		db.execSQL("insert into "+ tbChatList+" select *,' ' from tb_chat_List_tmp");
		db.execSQL("DROP TABLE tb_chat_List_tmp");//删除临时表
		//房间列表
		db.execSQL("ALTER TABLE "+tbRoomList+" RENAME TO tb_room_List_tmp");
		db.execSQL("CREATE TABLE if not exists "
				+ tbRoomList
				+ "(_id INTEGER PRIMARY KEY AUTOINCREMENT, login_uid INTEGER, room_id INTEGER,master_uid INTEGER ,is_group INTEGER,title TEXT," +
				"  mtime INTEGER,self_index INTEGER,content TEXT,type TEXT,from_uid INTEGER,from_uname TEXT,from_uface TEXT,from_uface_url TEXT," +
				"to_name TEXT,to_uid INTEGER,member_num INTEGER,isNew INTEGER, group_face TEXT, logo_id INTGER,remark TEXT)");
		db.execSQL("insert into "+ tbRoomList+" select *,' ' from tb_room_List_tmp");
		db.execSQL("DROP TABLE tb_room_List_tmp");//删除临时表


	}
	
	
	public void createRoomTable() {
		this.getWritableDatabase().execSQL("CREATE TABLE if not exists "
				+ tbRoomList
				+ "(_id INTEGER PRIMARY KEY AUTOINCREMENT,room_id INTEGER,master_uid INTEGER ,is_group INTEGER,title TEXT,"
				+ " mtime INTEGER,self_index INTEGER,content TEXT,type TEXT,from_uid INTEGER,from_uname TEXT,from_uface TEXT,from_uface_url TEXT,to_name TEXT,to_uid INTEGER,member_num INTEGER,isNew INTEGER)");
	}
	
}
